package com.dfhz.ken.zermall.utils.network;

import com.dfhz.ken.zermall.utils.L;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.List;

public class FabuhttpClient {
	public String faburubbishifo(List<NameValuePair> rubbishifo, String path) throws ClientProtocolException, IOException
	{
        for(int i = 0;i<rubbishifo.size();i++){
            L.e("POST参数："+rubbishifo.get(i).getName(),rubbishifo.get(i).getValue());
        }
	String result="";
	HttpClient httpclient=new DefaultHttpClient();
	HttpPost httppost=new HttpPost(path);
	try {
	httppost.setEntity(new UrlEncodedFormEntity(rubbishifo,"UTF-8"));
	HttpResponse httpResponse=httpclient.execute(httppost);
	 // 显示响应
    result = showResponseResult(httpResponse);
    return result;
	} catch (UnsupportedEncodingException e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
	}
	L.e("POST返回：",result);
	return result;
	}
	
	
	/**
     * 显示响应结果到命令行和TextView
     * @param response
     */
    private static String showResponseResult(HttpResponse response)
    {
    	String result ="";
        if (null == response)
        {
            return result;
        }

        HttpEntity httpEntity = response.getEntity();
        try
        {
            InputStream inputStream = httpEntity.getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
            String line = "";
            while (null != (line = reader.readLine()))
            {
                result += line;

            }
			return result;
           
            
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return result;
        }

    }
}
